package com.jiesen.base.modules.company.controller;

import com.jiesen.base.modules.company.model.JsPublicUsersRecord;
import com.jiesen.base.modules.company.service.IJsPublicUsersRecordService;
import com.jiesen.base.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.jiesen.base.modules.company.model.JsPublicUsersCall;
import com.jiesen.base.modules.company.service.IJsPublicUsersCallService;
import com.jiesen.base.utils.R;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bao
 */

@RestController
@RequestMapping(value = "/jsPublicUsersCall", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class JsPublicUsersCallController {
	
	@Autowired
	private IJsPublicUsersCallService jsPublicUsersCallService;

	@Autowired
	private IJsPublicUsersRecordService jsPublicUsersRecordService;

	//根据条件分页查询
	@RequestMapping(value = "/getList",method = RequestMethod.POST)
	@ResponseBody
	public R getList(HttpServletRequest request, @RequestBody JsPublicUsersCall jsPublicUsersCall) {
		String token = request.getHeader("token");
		Integer id = Integer.valueOf(JWTUtil.getUserId(token));
		List<JsPublicUsersRecord> jsPublicUsersRecords = jsPublicUsersRecordService.selectByUserIdAndPublicId(id, jsPublicUsersCall.getId());
		if(jsPublicUsersRecords.size()<=0){
			return R.error("我的客户里没有该客户");
		}
		JsPublicUsersRecord jsPublicUsersRecord = jsPublicUsersRecords.get(0);
		Integer id1 = jsPublicUsersRecord.getId();
		jsPublicUsersCall.setId(null);
		jsPublicUsersCall.setRecordId(id1);
		R page = jsPublicUsersCallService.page(jsPublicUsersCall);
		return page;
	}

	//增加一个通话记录
	@RequestMapping(value = "/addJsPublicUsersCall",method = RequestMethod.POST)
	@ResponseBody
	public R addJsPublicUsersCall(HttpServletRequest request, @RequestBody JsPublicUsersCall jsPublicUsersCall) {
		String token = request.getHeader("token");
		Integer id = Integer.valueOf(JWTUtil.getUserId(token));
		List<JsPublicUsersRecord> jsPublicUsersRecords = jsPublicUsersRecordService.selectByUserIdAndPublicId(id, jsPublicUsersCall.getId());
		if(jsPublicUsersRecords.size()<=0){
			return R.error("我的客户里没有该客户");
		}
		JsPublicUsersRecord jsPublicUsersRecord = jsPublicUsersRecords.get(0);
		Integer id1 = jsPublicUsersRecord.getId();
		jsPublicUsersCall.setId(null);
		jsPublicUsersCall.setRecordId(id1);
		jsPublicUsersCallService.save(jsPublicUsersCall);
        return R.ok();
    }

}
